<html>
<head>
<script type="text/javascript">

function loaded() {
  // navigator.webkitPersistentStorage.requestQuota(1024*1024);
  document.getElementById('nacl').addEventListener('message', handleMessage,
    false);
}

function handleMessage(ev) {
  var kContext = "context:";
  var index = ev.data.indexOf(kContext);
  if (index >= 0) {
    index += kContext.length;
    console.log("received", JSON.parse(ev.data.substr(index)));
  } else {
    console.log("received", ev.data);
  }
}

function clicked() {
  var node = document.getElementById('nacl');
  node.postMessage("key:j");
  node.postMessage("key:i");
  node.postMessage("key:3");
  node.postMessage("key:g");
  node.postMessage("key:4");
  node.postMessage("key:Down");
  node.postMessage("key:Right");
  node.postMessage("key:Right");
  node.postMessage("key:3");
  node.postMessage("key:Left");
  node.postMessage("key:Left");
  node.postMessage("key:Left");
  node.postMessage("key:Down");
  node.postMessage("key:1");
  node.postMessage("key:5");
  node.postMessage("key:j");
  node.postMessage("key:/");
  node.postMessage("key: ");
  node.postMessage("key:Enter");
}
</script>
</head>
<body onload="loaded();">
<h1> Chewing NaCl test </h1>
<embed name="nacl_module" width="0" height="0" type="application/x-nacl"
       id="nacl" src="newlib/Debug/chewing.nmf" />
<input type="button" onClick="clicked();" value="Click Test"></input>
</body>
</html>
